package com.ease.juc.utils;

/**
 * 原因：
 * 1. 指令重排
 * 2. volatile只能解决可见性，不能解决原子性
 *
 * @author: chenhs
 * @date: Created in 15:58 2021/10/17
 **/
public class CounterBasic implements Counter {

    private volatile int i = 0;
    //private int i = 0;

    @Override
    public void incr() {
        int current = i;

        int result = current + 1;

        i = result;
        //i++;
    }

    @Override
    public void desc() {
        i--;
    }

    @Override
    public Integer getResult() {
        return i;
    }
}
